#include "screen.h"
#include "i8259.h"
#include "multiboot.h"
#include "panic.h"
#include <types.h>
#include "memory.h"
#include "buddy.h"

#define CHECK_FLAG(flags,bit)   ((flags) & (1 << (bit)))

void init_paging(multiboot_info_t* mbt);
void newgdt();
int printk(const char* fmt, ...);
void mm_init(multiboot_info_t* mbt);
//void kmain ()  __attribute__((stdcall)); 

void __attribute__((cdecl)) kmain(multiboot_info_t* mbt)
{
     clear();

     mm_init(mbt);
     pic_init();

     putc('\n');
     static char str[] = "Hello, World";
     printk("\nMULTIBOOT arg: %x\n", mbt);

     extern struct buddy_class buddy_kmalloc_class;
     void* ptr1, *ptr2, *ptr3;
     ptr1 = kmalloc(4);
     buddy_bucket_dump(buddy_kmalloc_class.bucket, buddy_kmalloc_class.bucket_total);     
     ptr2 = kmalloc(1);
     buddy_bucket_dump(buddy_kmalloc_class.bucket, buddy_kmalloc_class.bucket_total);
     
     ptr3 = kmalloc(9);
     buddy_bucket_dump(buddy_kmalloc_class.bucket, buddy_kmalloc_class.bucket_total);
     printk("ptr1=0x%x, ptr2=0x%x, ptr3=0x%x\n", ptr1, ptr2, ptr3);

     kfree_s(ptr1, 4);
     buddy_bucket_dump(buddy_kmalloc_class.bucket, buddy_kmalloc_class.bucket_total);
     kfree_s(ptr3, 16);
     buddy_bucket_dump(buddy_kmalloc_class.bucket, buddy_kmalloc_class.bucket_total);
     kfree_s(ptr2, 4);
     buddy_bucket_dump(buddy_kmalloc_class.bucket, buddy_kmalloc_class.bucket_total);
     
     extern int end;
     printk("\nKernel end: %x, %s\n%c, %d, %%\n", &end, str, 'X', 1111);
     extern int MEMTOP;
     printk("MEMTOP: 0x%x, page dir: %d\n", MEMTOP, MEMTOP/(1024*4*1024));
//     panic("we reached the end!\n");
     while(1);
    

}


